<?php

    class statistics_model extends CI_Model {
    
        public function get_data() { 
            return false;
        }
        
        public function count_cesty($now) {
            $queries = array($this->db->query("SELECT * FROM odcestovani"), $this->db->query("SELECT * FROM pricestovani"));
            $result = array();
            
            foreach ($queries as $key => $value) {
                $result[$key] = 0;
                foreach($value->result_array() as $array) {
                    $ok = true;
                    if ($now) {
                        $datum_odchodu = explode("-", $array["date_of_arrival"]);
                        $datum_prichodu = explode("-", $array["date_of_departure"]);
                        $datum_teraz = explode("-", date("Y-m-d")); 
                        $ok = false;
                        if ((int)$datum_teraz[0] >= (int)$datum_odchodu[0] && (int)$datum_teraz[0] <= (int)$datum_prichodu[0]) {
                            if ((int)$datum_teraz[0] == (int)$datum_odchodu[0] && (int)$datum_teraz[0] == (int)$datum_prichodu[0] && (int)$datum_teraz[1] >= (int)$datum_odchodu[1] && (int)$datum_teraz[1] <= (int)$datum_prichodu[1]) {
                                if ((int)$datum_teraz[1] == (int)$datum_odchodu[1] && (int)$datum_teraz[1] == (int)$datum_prichodu[1] && (int)$datum_teraz[2] >= (int)$datum_odchodu[2] && (int)$datum_teraz[2] <= (int)$datum_prichodu[2]) {
                                    $ok = true;
                                }
                                else if ((int)$datum_odchodu[1] != (int)$datum_prichodu[1]) {
                                    $ok = true;
                                }
                            }
                            else if ((int)$datum_odchodu[0] != (int)$datum_prichodu[0]) {
                                $ok = true;
                            }
                        }
                    }
                    
                    if ($ok) {
                        $result[$key]++;
                    }
                }
            }
            
            return array("Služobné cesty odcestovaní" => $result[0], "Služobné cesty pricestovaní" => $result[1]);
        }
        
        public function count_ludia_na_cestach($now) {
            $queries = array($this->db->query("SELECT * FROM odcestovani"), $this->db->query("SELECT * FROM pricestovani"));
            $result = array();
            
            foreach ($queries as $key => $value) {
                $result[$key] = 0;
                foreach($value->result_array() as $array) {
                    $ok = true;
                    if ($now) {
                        $datum_odchodu = explode("-", $array["date_of_arrival"]);
                        $datum_prichodu = explode("-", $array["date_of_departure"]);
                        $datum_teraz = explode("-", date("Y-m-d")); 
                        $ok = false;
                        if ((int)$datum_teraz[0] >= (int)$datum_odchodu[0] && (int)$datum_teraz[0] <= (int)$datum_prichodu[0]) {
                            if ((int)$datum_teraz[0] == (int)$datum_odchodu[0] && (int)$datum_teraz[0] == (int)$datum_prichodu[0] && (int)$datum_teraz[1] >= (int)$datum_odchodu[1] && (int)$datum_teraz[1] <= (int)$datum_prichodu[1]) {
                                if ((int)$datum_teraz[1] == (int)$datum_odchodu[1] && (int)$datum_teraz[1] == (int)$datum_prichodu[1] && (int)$datum_teraz[2] >= (int)$datum_odchodu[2] && (int)$datum_teraz[2] <= (int)$datum_prichodu[2]) {
                                    $ok = true;
                                }
                                else if ((int)$datum_odchodu[1] != (int)$datum_prichodu[1]) {
                                    $ok = true;
                                }
                            }
                            else if ((int)$datum_odchodu[0] != (int)$datum_prichodu[0]) {
                                $ok = true;
                            }
                        }
                    }
                    
                    if ($ok) {
                        $arr = array();
                        if ($array["person_id"] != "") {
                            $arr = explode(",", $array["person_id"]);
                        }
                        $result[$key] += count($arr);
                    }
                }
            }
            
            return array("Odcestovaní ľudia" => $result[0], "Pricestovaní ľudia" => $result[1]);
        }
        
        public function count_men_women($table, $now) {
            $query = $this->db->query("SELECT * FROM " . $table);
            
            $men = 0;
            $women = 0;
            foreach($query->result_array() as $array) {
                $ok = true;
                if ($now) {
                    $datum_odchodu = explode("-", $array["date_of_arrival"]);
                    $datum_prichodu = explode("-", $array["date_of_departure"]);
                    $datum_teraz = explode("-", date("Y-m-d")); 
                    $ok = false;
                    if ((int)$datum_teraz[0] >= (int)$datum_odchodu[0] && (int)$datum_teraz[0] <= (int)$datum_prichodu[0]) {
                        if ((int)$datum_teraz[0] == (int)$datum_odchodu[0] && (int)$datum_teraz[0] == (int)$datum_prichodu[0] && (int)$datum_teraz[1] >= (int)$datum_odchodu[1] && (int)$datum_teraz[1] <= (int)$datum_prichodu[1]) {
                            if ((int)$datum_teraz[1] == (int)$datum_odchodu[1] && (int)$datum_teraz[1] == (int)$datum_prichodu[1] && (int)$datum_teraz[2] >= (int)$datum_odchodu[2] && (int)$datum_teraz[2] <= (int)$datum_prichodu[2]) {
                                $ok = true;
                            }
                            else if ((int)$datum_odchodu[1] != (int)$datum_prichodu[1]) {
                                $ok = true;
                            }
                        }
                        else if ((int)$datum_odchodu[0] != (int)$datum_prichodu[0]) {
                            $ok = true;
                        }
                    }
                }
                
                if ($ok) {
                    $person_ids = array();
                    if ($array['person_id'] != "") {
                        $person_ids = explode(",", $array['person_id']);
                    }
                    
                    foreach($person_ids as $person_id) {
                        $query_people = $this->db->query("SELECT `sex` FROM `people` WHERE `person_id`=" . $person_id);
                        $result_array = $query_people->result_array();
                        
                        if (isset($result_array[0])) {
                            if ($result_array[0]['sex'] == "muz") {
                                $men++;
                            }
                            else if ($result_array[0]['sex'] == "zena") {
                                $women++;
                            }
                        }
                    }
                }
            }
            
            return array("Muži" => $men, "Ženy" => $women);
        }
        
        public function count_items($table, $column, $now) {
            $query = $this->db->query("SELECT * FROM " . $table);
            
            $stat = array();
            foreach($query->result_array() as $array) {
                $ok = true;
                if ($now) {
                    $datum_odchodu = explode("-", $array["date_of_arrival"]);
                    $datum_prichodu = explode("-", $array["date_of_departure"]);
                    $datum_teraz = explode("-", date("Y-m-d")); 
                    $ok = false;
                    if ((int)$datum_teraz[0] >= (int)$datum_odchodu[0] && (int)$datum_teraz[0] <= (int)$datum_prichodu[0]) {
                        if ((int)$datum_teraz[0] == (int)$datum_odchodu[0] && (int)$datum_teraz[0] == (int)$datum_prichodu[0] && (int)$datum_teraz[1] >= (int)$datum_odchodu[1] && (int)$datum_teraz[1] <= (int)$datum_prichodu[1]) {
                            if ((int)$datum_teraz[1] == (int)$datum_odchodu[1] && (int)$datum_teraz[1] == (int)$datum_prichodu[1] && (int)$datum_teraz[2] >= (int)$datum_odchodu[2] && (int)$datum_teraz[2] <= (int)$datum_prichodu[2]) {
                                $ok = true;
                            }
                            else if ((int)$datum_odchodu[1] != (int)$datum_prichodu[1]) {
                                $ok = true;
                            }
                        }
                        else if ((int)$datum_odchodu[0] != (int)$datum_prichodu[0]) {
                            $ok = true;
                        }
                    }
                }
            
                if ($ok) {
                $item = $array[$column];
                    if (isset($stat[strtolower($item)])) {
                        $stat[strtolower($item)]++;
                    }
                    else {
                        $stat[$item] = 1;
                    }
                }
            }

            return $stat;
        }

    }
    
?>